class HwInfo:

    """
    self.hwDict example:
    {
        "mobo": {
            "vendor": "Asrock",
            "model": "X399 Fatality Professional Gaming",
        },
        "cpu": {
            "vendor": "Intel",
            "model": "i7-4600U",
            "cores": 4,
        },
        "memory: {
            "size": 16,             # in GiB
        },
        "gpu": {


        },
        "worker-gpu": {


        },
        "fan": {
            "only-fan": {
                "model": "Embedded",
                "power": "weak",
            },
        }

    }
    """

    def __init__(self):
        self.arch = None                      # str
        self.chassis_type = None              # ChassisType
        self.hwDict = None                    # dict
        self.kernelCfgRules = None            # ordered-dict(section-name,section-content)
        self.useFlags = None                  # ordered-dict(section-name,section-content)
        self.grubExtraWaitTime = None         # int


class HwInfoPcBranded(HwInfo):

    def __init__(self):
        super().__init__()

        self.vendor = None
        self.model = None
        self.serialNumber = None
        self.hwSpec = None                    # dict
        self.changeList = []


class _PcAliyun:

    _MODELS = [
        "ecs.t1.small",
    ]

    def info(self):
        self.manu = Util.dmiDecodeWithCache("system-manufacturer")
        if self.manu != "Alibaba Cloud":
            return None
        assert Util.dmiDecodeWithCache("system-product-name") == "Alibaba Cloud ECS"

        self.sn = Util.dmiDecodeWithCache("system-serial-number")

        self.model = Util.getMachineInfoWithCache("ALIYUN-HWNAME")
        assert self.model in self._MODELS

        ret = HwInfoPcBranded()
        ret.vendor = "ALIYUN"
        ret.model = self.model
        ret.hwSpec = self._hwSpec()
        ret.serialNumber = self.sn
        ret.arch = "amd64"
        ret.chassis_type = ChassisType.COMPUTER
        ret.hwDict = self._hwDict(ret.hwSpec)
        ret.changeList = self._changeList(ret.hwSpec, ret.hwDict)
        ret.kernelCfgRules = self._kernelCfgRules()
        ret.useFlags = self._useFlags()
        ret.grubExtraWaitTime = 20
        return ret

    def _hwSpec(self):
        ret = {
        }

        if self.model == "ecs.t1.small":
            return ret
        assert False

    def _hwDict(self, hwSpec):
        ret = _UtilHwDict.get(hwSpec)
        if "sensor" in ret:
            del ret["sensor"]

        return ret

    def _changeList(self, origHwDict, hwDict):
        return []

    def _kernelCfgRules(self):
        ret = _Util.kernelCfgRules()
        ret = _Util.kernelCfgRulesNoPowerSave(ret)
        if self.model == "ecs.t1.small":
            return ret
        assert False

    def _useFlags(self):
        ret = _Util.getUseFlags("amd64")
        ret.update(_Util.getUseFlags3())
        return ret



class _Util:

    @staticmethod
    def kernelCfgRulesNoPowerSave(kernelCfgRules):
        if True:
            rname = "hardware-management-and-monitor"

            buf = ""
            buf += "ACPI_PROCESSOR=n\n"
            buf += "[symbols:ACPI]=m,y\n"
            kernelCfgRules[rname] = kernelCfgRules[rname].replace("[symbols:ACPI]=m,y\n", buf)

            kernelCfgRules[rname] = kernelCfgRules[rname].replace("[symbols:/Power management and ACPI options/CPU Frequency scaling]=y\n", "")
            kernelCfgRules[rname] = kernelCfgRules[rname].replace("INTEL_IDLE=y\n", "")
            kernelCfgRules[rname] = kernelCfgRules[rname].replace("[symbols:/Power management and ACPI options/CPU Idle]=y\n", "")

        return kernelCfgRules

    @staticmethod
    def kernelCfgRulesForOnlyNewestIntelCpu(kernelCfgRules):
        if True:
            rname = "hardware-management-and-monitor"

            toBeReplaced = "[symbols:/Power management and ACPI options/CPU Frequency scaling]=y\n"
            buf = ""
            buf += "CPU_FREQ=y\n"
            buf += "CPU_FREQ_STAT=y\n"
            buf += "X86_INTEL_PSTATE=y\n"
            buf += "/Power management and ACPI options/CPU Frequency scaling/CPU Frequency scaling/Default CPUFreq governor=CPU_FREQ_DEFAULT_GOV_PERFORMANCE\n"
            buf += "[symbols:/Power management and ACPI options/CPU Frequency scaling]=n\n"
            kernelCfgRules[rname] = kernelCfgRules[rname].replace(toBeReplaced, buf)

        return kernelCfgRules

    @staticmethod
    def getUseFlags3():
        ret = OrderedDict()

        if True:
            buf = ""
            buf += "# support only common video standard\n"
            buf += "*/* VIDEO_CARDS: -* vesa\n"
            ret["graphics"] = buf

        return ret
